package net.dgg.rz.production.service;

import com.alibaba.fastjson.JSONObject;
import net.dgg.framework.tac.utils.DggValidateUtil;
import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.iboss.base.util.JsonUtil;
import net.dgg.rz.common.base.service.BaseService;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.production.dao.OrderJsonDao;
import net.dgg.rz.production.dao.ProductOrderDao;
import net.dgg.rz.production.entity.OrderJson;
import net.dgg.rz.production.entity.dto.UpdateGsscOrderDto;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.tmd.foundation.platform.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * Created by zyou on 2018/12/14.
 */
@Service
public class OrderJsonService extends BaseService{
    @Autowired
    private UserService userservice;
    @Autowired
    private CommonMangerExtService commonMangerExtService;
    @Autowired
    private OrderJsonDao orderJsonDao;
    @Autowired
    private ProductOrderDao productOrderDao;



    /**
     * 保存一条json数据
     * @param returnUrl
     * @param notifyUrl
     * @param order
     */
    @Transactional(rollbackFor = Exception.class)
    public String saveOrderJson(String returnUrl, String notifyUrl, String order,String products) {
        DggValidateUtil.notNull(returnUrl, CommonExcption.class,"返回url为空");
        DggValidateUtil.notNull(notifyUrl, CommonExcption.class,"通知url为空");
        DggValidateUtil.notNull(order, CommonExcption.class,"业务数据为空");
        DggValidateUtil.notNull(products, CommonExcption.class,"产品数据为空");
        Map orderMap = JsonUtil.json2Obj(order, Map.class);
        Long orderId = Long.parseLong(orderMap.get("orderId").toString());
        String orderNo = orderMap.get("orderNo").toString();
        OrderJson orderJson = orderJsonDao.selectByOrderNo(orderNo);
        if(orderJson != null && orderJson.getFlag()==1){
            DggValidateUtil.notNull(null,CommonExcption.class,"当前订单已经生成生产单，请勿重复下单！");
        }
        Long curentUserId = 1L;//当前登录用户id,超级管理员
        UserEntity userentity;//当前登录人
        Map curentDept;//当前登录人部门
        if (!StringUtils.isEmpty(orderMap.get("createrId"))){
            //传过来的创建者
            userentity = userservice.findUserById(Long.valueOf(orderMap.get("createrId").toString()));
            curentDept = this.commonMangerExtService.findOrg(userentity.getOrgId());
        }else {
            //超级管理员
            userentity = userservice.findUserById(curentUserId);
            curentDept = this.commonMangerExtService.findOrg(userentity.getOrgId());
        }

        if(orderJson != null && (orderJson.getFlag()==0||orderJson.getFlag()==2)){
            orderJson.setOrderJson(order);
            orderJson.setOrderBusinessJson(products);
            orderJson.setReturnUrl(returnUrl);
            orderJson.setNotifyUrl(notifyUrl);
            orderJson.setFlag(2);
            this.updateOpt(orderJson,userentity);
            orderJson.setUpdaterOrgName(null == curentDept ? null : curentDept.get("name").toString());
            orderJsonDao.updateByPrimaryKeySelective(orderJson);
        }
        if(orderJson == null){
            orderJson = new OrderJson();
            orderJson.setId(DggKeyWorker.nextId());
            orderJson.setOrderId(orderId);
            orderJson.setOrderNo(orderNo);
            orderJson.setOrderJson(order);
            orderJson.setOrderBusinessJson(products);
            orderJson.setReturnUrl(returnUrl);
            orderJson.setNotifyUrl(notifyUrl);
            orderJson.setFlag(2);
            this.saveOpt(orderJson,userentity);
            orderJson.setCreaterOrgName(null == curentDept ? null : curentDept.get("name").toString());
            orderJsonDao.insertSelective(orderJson);
        }
        return orderNo;


    }

    /**
     * 通过订单编号查询
     * @param orderNo
     * @return
     */
    public OrderJson selectByOrderNo(String orderNo) {
        return orderJsonDao.selectByOrderNo(orderNo);
    }
    /**
     * 通过订单id查询
     * @param orderId
     * @return
     */
    public OrderJson selectByOrderId(Long orderId) {
        return orderJsonDao.selectByOrderId(orderId);
    }
    /**
     * 订单中心修改合同编号
     * @param orderId
     * @param contractNo
     */
    @Transactional(rollbackFor = Exception.class)
    public void executeUpdateContractNo(Long orderId, String contractNo) {
        UpdateGsscOrderDto updateGsscOrder = new UpdateGsscOrderDto();
        updateGsscOrder.setOrderId(orderId);
        updateGsscOrder.setContractNo(contractNo);
        //修改order
       orderJsonDao.updateOrderInfo(updateGsscOrder);
        //修改product_order
        productOrderDao.updateOrderInfo(updateGsscOrder);
    }

    /**
     * 订单中心修改签单人信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void executeUpdateSignUser(JSONObject jsonObject) {
        UpdateGsscOrderDto updateGsscOrder = new UpdateGsscOrderDto();
        updateGsscOrder.setOrderId(jsonObject.getLong("orderId"));
        updateGsscOrder.setSignUserId(jsonObject.getLong("signUserId"));
        updateGsscOrder.setSignUserName(jsonObject.getString("signUserName"));
        updateGsscOrder.setSignUserOrgId(jsonObject.getLong("signUserOrgId"));
        updateGsscOrder.setSignUserOrgName(jsonObject.getString("signUserOrgName"));
        updateGsscOrder.setSignOrgId(jsonObject.getLong("signOrgId"));
        updateGsscOrder.setSignOrgName(jsonObject.getString("signOrgName"));
        //修改product_order
        productOrderDao.updateOrderInfo(updateGsscOrder);
    }

}
